﻿

//如果前者和后者有相同的属性，则以前者为主。
function mergeObjectBefore() {
    var object = new Object();
    for (i in arguments) {
        var objectTemp = arguments[i];
        for (x in objectTemp) {
            if (!object[x]) {
                object[x] = objectTemp[x];
            }
        }
    };
    return object;
}

//如果前者和后者有相同的属性，则以后者为主。
function mergeObjectAfter() {
    var object = new Object();
    for (i in arguments) {
        var objectTemp = arguments[i];
        for (x in objectTemp) {
            object[x] = objectTemp[x];
        }
    };
    return object;
}

function trimStrSpace(str) {
    var tempStr = new String();
    var spaceStart = /^\s+/gim;
    var spaceEnd = /\s+$/gim;
    tempStr = str.replace(spaceStart, "");
    tempStr = tempStr.replace(spaceEnd, "");
    return tempStr;
}


function changeArrayToObject(array, splitor) {
    var object = new Object();
    for (x in array) {
        if (array[x].length != 0 && array[x].indexOf(splitor) != -1) {
            var arrayProperty = array[x].split(splitor);
            object[trimStrSpace(arrayProperty[0])] = trimStrSpace(arrayProperty[1]);
        }
    }
    return object;
}

var Convert = {
    jsonObjectToString: function (object) {
        return JSON.stringify(object);
    },
    jsonStringToObject: function (str) {
        return JSON.parse(str);
    },
    strToInt: function () {
        var result = null;
        //        var is16 = false;
        //        var is8Or16 = false;
        //        var regexp16 = /^(0x)/gim;
        //        var regexp8Or16 = /^0/;
        //        if (str.indexOf(regexp16) != -1) {
        //            is16 = true;
        //        }
        //        if (str.indexOf(regexp8Or16) != -1) {
        //            is8Or16 = true;
        //        }

        //        if (is16) {
        //            result = parseInt(str.replace(regexp16, ""),16);
        //        } else if (is8Or16) {
        //            result=parseInt(str,str.re
        //        }
        //        else {
        //        }
        if (arguments.length == 2) {
            result = parseInt(arguments[0], arguments[1]);
        }
        else {
            result = parseInt(arguments[0]);
        }

        if (isNaN(result)) {
            throw new Error("Invalid String");
        }
        else {
            return result;
        }
    },
    strToFloat: function () {
        var result = null;
        result = parseFloat(arguments[0]);
        if (isNaN(result)) {
            throw new Error("Invalid String");
        }
        else {
            return result;
        }
    },
    otherToString: function () {
        return arguments[0].toString();
    }
};